import { Component } from '@angular/core';

import { DataPage } from '../data/data';
import { ContactPage } from '../contact/contact';
import { WarnPage } from '../warn/warn';
import { IntroductionPage } from '../introduction/introduction';

import 'rxjs/add/operator/map';
import { StompService, StompState } from '@stomp/ng2-stompjs';
import { Observable } from 'rxjs/Observable';
import { Message } from '@stomp/stompjs';
import * as moment from 'moment';

//推送
import { JPush } from '@jiguang-ionic/jpush';
import { Device } from '@ionic-native/device';

@Component({
  selector: 'tabs',
  templateUrl: 'tabs.html'
})
export class TabsPage {


  tab1Root = WarnPage;
  tab2Root = DataPage;
  tab3Root = ContactPage;
  tab4Root = IntroductionPage;


  message4$;
  subscription4$;
  subscribed: boolean;
  state$: Observable<any>;

  time;
  message;
  level;
  text;
  target;
  typeName;
  color;
  loweruppe;

  devicePlatform: string;

  constructor(
    private stompService: StompService,
    public directives: StompService,
    //推送
    public jpush: JPush, device: Device
  ) {

    //推送
    this.devicePlatform = device.platform;
    
        jpush.init()
          .then(res => {
            alert(res)
    
            document.addEventListener('jpush.receiveNotification', (event: any) => {
              if (this.devicePlatform == 'Android') {
              } else {
              }
              alert('Receive notification: ' + JSON.stringify(event));
            }, false);
    
            document.addEventListener('jpush.openNotification', (event: any) => {
              if (this.devicePlatform == 'Android') {
              } else {
                if (event.aps == undefined) {
                } else {
                }
              }
              alert('open notification: ' + JSON.stringify(event));
            }, false);
    
            document.addEventListener('jpush.receiveLocalNotification', (event: any) => {
              if (this.devicePlatform == 'Android') {
              } else {
              }
              alert('receive local notification: ' + JSON.stringify(event));
            }, false);
          })
          .catch(res => {
            alert(res)
          });
        jpush.setDebugMode(true);
  }

  ionViewDidLoad() {
    this.subscribe();

    //推送
    // setTimeout(_=>{
    //   var mes = "告警时间"+":"+ this.time+","+"告警级别"+":"+ this.level+","+"告警信息"+":"+this.message
    //   this.addLocalNotification(mes)
    // },2000)
  }
  addLocalNotification(mes) {
    if (this.devicePlatform == 'Android') {
      this.jpush.addLocalNotification(0, mes, '告警', 1, 1000);
    } else {
      console.log(111)
      this.jpush.addLocalNotificationForIOS(5, 'Hello JPush', 1, 'localNoti1');
    }
  }

  subscribe() {
    if (this.subscribed) {
      return;
    }
    // STOMP状态
    this.state$ = this.stompService.state
      .map((state: number) => StompState[state]);

    this.message4$ = this.stompService
      .subscribe(`/topic/warn`);
    this.subscription4$ =
      this.message4$
        .map((message: Message) => JSON.parse(message.body, (k, v) => {
          if (k === 'time') {
            return (moment(v, 'YYYYMMDDHHmmssSSS').format("YYYY-MM-DD HH:mm:ss"));
          }
          return v;
        }))
        .subscribe(data => {

          console.log(888888, data);

          //告警时间
          this.time = data['time'];
          //告警级别
          this.level = data['level']
          //测点名称
          this.target = data['target'].name
          //测点类型
          this.typeName = data['target'].typeName
          //告警颜色
          this.color = data['threshold'].name
          //阈值范围
          this.loweruppe = "[" + data['threshold'].lower + ',' + data['threshold'].upper + "]"
          //告警信息当前监测数据
          this.message = data['message']

          console.log(2222222222, this.time);

          if(this.message !=undefined ){
            var mes = "告警时间"+":"+ this.time+","+"告警等级"+":"+ this.color+","+"告警信息"+":"+this.message+','+'点击查看详情...'
            this.addLocalNotification(mes)
          }
         
        });


    this.subscribed = true;
  }

  unsubscribe() {
    if (!this.subscribed) {
      return;
    }

    // this.subscription$.unsubscribe();

    this.subscription4$.unsubscribe();

    this.subscription4$ = null;

    this.message4$ = null;


    this.subscribed = false;
  }


  click() {
    this.time = undefined;
  }





}
